package com.example.pjy.controller;

import com.example.pjy.pojo.Result;
import com.example.pjy.pojo.Rules;
import com.example.pjy.pojo.Student;
import com.example.pjy.service.RulesService;
import com.example.pjy.service.StudentService;
import jakarta.annotation.Resource;
import jdk.jfr.Category;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//告诉spring是一个控制层
@RequestMapping("/rules")//URL总路径叫User
public class RulesController {
    @Resource
    private RulesService rulesService;//注入UserService接口



    //把接收到的json转为实体类对象 运行时给的是json
    @PostMapping("/add")
    public Result add(@RequestBody Rules rules){
        rulesService.add(rules);
        return Result.success();
    }

    //查询 有主体数据给浏览器响应 主题数据是数组 数组里有多个对象
    @GetMapping("/selectlist")
    public Result<List<Rules>> list(){
        List<Rules> ru= rulesService.list();
        return Result.success(ru);//结果是数组
    }


    //根据id找学生
    @GetMapping("/detail")
    public Result<Rules> detail(Integer year){//student响应给前端浏览器是json  id接收前端传来的id
        Rules ru = rulesService.findByYear(year);
        return Result.success(ru);//结果是对象
    }


    //把json转为对象
    @PutMapping("/updateru")
    public Result updateru(@RequestBody Rules rules){
        rulesService.updateru(rules);
        return Result.success();
    }


    @DeleteMapping("/delete/{rulesId}")
    public Result delete(@PathVariable("rulesId") Integer rulesId){
        if (rulesId == null) {
            return Result.error("规则ID不能为空");
        }
        rulesService.delete(rulesId);
        return Result.success("删除成功");
    }
}
